package org.hy.pay.common.core.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PageBean<T> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int currentPage;//当前页
	private int numPerPage;// 每页显示多少条
	//查询数据
	private int totalCount;//总记录数
	private List<T> recordList = new ArrayList<T>(0);
	//计算
	private int totalPage;//总页数
	private int beginPageIndex;// 页码列表的开始索引
	private int endPageIndex;// 页码结束的索引（包含）
	
	private Map<String,Object> countResultMap;// 当前分页下的统计结果
	/**
	 * 计算总页数
	 * @param totalCount
	 * 			总记录数
	 * @param numPerPage
	 * 			每页记录数
	 * @return totalCount 总页数
	 */
	public static int countTotalPage(int totalCount,int numPerPage){
		if(totalCount % numPerPage == 0 ){
			return totalCount / numPerPage;
		}else{
			return totalCount / numPerPage+1;
		}
	}
	
	public static int checkCurrentPage(int totalCount,int numPerPage,int currentPage){
		int totalPage = PageBean.countTotalPage(totalCount, numPerPage);
		if(currentPage > totalPage){
			//如果页面提交的页数大于总页数，则当前页设为总页数
			if(totalPage < 1){
				return 1;
			}
			return totalPage;
		}else if(currentPage <1){
			return 1;
		}else{
			return currentPage;
		}
	}
	/**
	 * 校验页面输入的每页的记录数 numPerPage 是否合法.</br>
	 * 1、当页面输入的每页记录数大于允许的最大的每页的记录数MAX_PAGE_SIZE时,返回MAX_PAGE_SIZE
	 * 2、如果numPerPage 小于1时,则返回默认的每页的记录数DEFAULT_PAGE_SIZE
	 * 
	 * @param numPerPage
	 * 			页面输入的每页的记录
	 * @return
	 */
	public static int checkNumPerPage(int numPerPage){
		if(numPerPage > PageParam.MAX_PAGE_SIZE){
			return PageParam.MAX_PAGE_SIZE;
		}else if(numPerPage < 1){
			return PageParam.DEFAULT_NUM_PER_PAGE;
		}else{
			return numPerPage;
		}
	}
	
	public PageBean(int currentPage,int numPerPage,int totalCount,
			List<T> recordList){
		
		this.currentPage = currentPage;
		this.numPerPage = numPerPage;
		this.totalCount =  totalCount;
		this.recordList = recordList;
		//计算总页码
		totalPage = (totalCount + numPerPage -1) / numPerPage;
		if(totalPage <= 10){
			//总页数不多于10页，则全部显示
			beginPageIndex =1;
			endPageIndex = totalPage;
		}else{
			//如果总页数多于10页，则显示当前页附近的共10个
			//当前附近的共10个页码
			beginPageIndex = currentPage -4;
			endPageIndex = currentPage + 5;
			if(beginPageIndex < 1){
				beginPageIndex = 1;
				endPageIndex = 10;
			}
			//后面的页码不足5个时，则显示最后的10个页码
			if(endPageIndex > totalPage){
				endPageIndex = totalPage;
				beginPageIndex = totalPage -10 + 1;
			}
		}
	}
	
	public PageBean(int currentPage,int numPerPage,int totalCount,
			List<T> recordList,Map<String,Object> countResultMap){
		this.currentPage =  currentPage;
		this.numPerPage =  numPerPage;
		this.totalCount = totalCount;
		this.recordList = recordList;
		this.countResultMap = countResultMap;
		//计算总页码
		totalPage = (totalCount +numPerPage -1) / numPerPage;
		if(totalPage <= 10){
			beginPageIndex = 1;
			endPageIndex = totalPage;
		}else{
			beginPageIndex = currentPage -4;
			endPageIndex = currentPage +5;
			if(beginPageIndex < 1){
				beginPageIndex =1;
				endPageIndex = 10;
			}
			if(endPageIndex > totalPage){
				endPageIndex = totalPage;
				beginPageIndex = totalPage -10 +1;
			}
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public List<T> getRecordList() {
		return recordList;
	}

	public void setRecordList(List<T> recordList) {
		this.recordList = recordList;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getBeginPageIndex() {
		return beginPageIndex;
	}

	public void setBeginPageIndex(int beginPageIndex) {
		this.beginPageIndex = beginPageIndex;
	}

	public int getEndPageIndex() {
		return endPageIndex;
	}

	public void setEndPageIndex(int endPageIndex) {
		this.endPageIndex = endPageIndex;
	}

	public Map<String, Object> getCountResultMap() {
		return countResultMap;
	}

	public void setCountResultMap(Map<String, Object> countResultMap) {
		this.countResultMap = countResultMap;
	}
	
	
	
}
